using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public class tbdiemdenDAL
    {
         private SqlConnection cn = null;
        public tbdiemdenDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbdiemdenDAL(tbdiemdenDAL thach)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbdiemden diemden)
        {
            SqlCommand cm = new SqlCommand("diemden_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tendiemden", diemden.Tendiemden);
            cm.ExecuteNonQuery();
        }
        public void Update(tbdiemden diemden)
        {
            SqlCommand cm = new SqlCommand("diemden_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiemden", diemden.Madiemden);
            cm.Parameters.AddWithValue("@tendiadiem", diemden.Tendiemden);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("diemden_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiemden", iD);
            cm.ExecuteNonQuery();
        }
        public tbdiemdencollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("diemden_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbdiemdencollection diemden1Collection = new tbdiemdencollection();
            while (dr.Read())
            {
                tbdiemden diemden = new tbdiemden();
                diemden.Madiemden= int.Parse(dr.GetValue(0).ToString());
                diemden.Tendiemden = dr.GetValue(1).ToString();
                diemden1Collection.Add(diemden);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return diemden1Collection;
        }
    }
}
